import { Logger } from "../engine/Logger";
import GameController from "../GameController";

export class ServerConfig {

    private static WebServer_ip: string = ""
    private static Port_WebServer: number = 8600
    public ip: string;
    public http_port: number;
    public socket_port: number;

    public static EnvironmentType = {
        Dev: 1,
        Test: 2,
        Online: 3,
    }

    public static environment: number = ServerConfig.EnvironmentType.Test
    
    public static init() {
        if (this.environment == ServerConfig.EnvironmentType.Dev) {
            this.WebServer_ip = "127.0.0.1"
            
        } else if (this.environment == ServerConfig.EnvironmentType.Test) {
            this.WebServer_ip = "121.37.251.120"
        } 
        else {
            this.WebServer_ip = "121.37.251.120"
        }
    }


    public static ServerType_List = {
        WebServer: "[WebServer]",
        account_server: "[account_server]",
        hall_server: "[hall_server]",
        game_server: "[game_server]",
        log_server: "[log_server]",
        pay_server:"[pay_server]",
    }

    private static serverMap: Map<string, ServerConfig> = new Map<string, ServerConfig>();

    public static socketIp: string;
    public static socketPort: number;

    public static initAccountServer(configObj: object) {
        this.serverMap.set(this.ServerType_List.account_server, configObj[this.ServerType_List.account_server])
        this.serverMap.set(this.ServerType_List.log_server, configObj[this.ServerType_List.log_server])
        this.serverMap.set(this.ServerType_List.pay_server, configObj[this.ServerType_List.pay_server])
        this.serverMap.set(this.ServerType_List.hall_server, configObj[this.ServerType_List.hall_server])
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.hall_server)
        this.setSocketInfo(serverConfig.ip, serverConfig.socket_port)
        Logger.log("ServerConfig=", this.serverMap)
    }

    public static initHallServer(configObj: object) {
        this.serverMap.set(this.ServerType_List.hall_server, configObj["server"])
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.hall_server)
        this.setSocketInfo(serverConfig.ip, serverConfig.socket_port)
        Logger.log("ServerConfig=", this.serverMap)
    }

    public static initGameServer(configObj: object) {
        this.serverMap.set(this.ServerType_List.game_server, configObj["server"])
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.game_server)
        this.setSocketInfo(serverConfig.ip, serverConfig.socket_port)
        Logger.log("initGameServer=", serverConfig)
    }

    public static getWebServerUrl() {
        return "http://" + this.WebServer_ip + ":" + this.Port_WebServer;
    }

    public static getLogServerUrl() {
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.log_server)
        return "http://" + serverConfig.ip + ":" + serverConfig.http_port;
    }

    public static getPayServerUrl(){
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.pay_server)
        return "http://" + serverConfig.ip + ":" + serverConfig.http_port;
    }

    public static getAccountServerUrl() {
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.account_server)
        return "http://" + serverConfig.ip + ":" + serverConfig.http_port;
    }

    public static getHallServerUrl() {
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.hall_server)
        return "http://" + serverConfig.ip + ":" + serverConfig.socket_port;
    }

    public static getHallServerConfig() {
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.hall_server)
        return serverConfig
    }

    public static setHallServerSocket() {
        let serverConfig: ServerConfig = this.serverMap.get(this.ServerType_List.hall_server)
        if(serverConfig){
            this.setSocketInfo(serverConfig.ip, serverConfig.socket_port)
        }else{
            GameController.instance.restartGame()
        }
    }

    public static setSocketInfo(ip: string, port: number) {
        this.socketIp = ip;
        this.socketPort = port;
    }

    public static getSocketUrl() {
        return "ws://" + this.socketIp + ":" + this.socketPort;
    }

}